import markdown,os,re


def md_to_html_with_styled_tables(md_content):
    # 转换Markdown为HTML（支持表格）
    html_table = markdown.markdown(md_content, extensions=['tables'])
    
    # 定义表格CSS样式（带边框）
    table_css = """
    <style>
        table {
            border-collapse: collapse;  /* 合并边框 */
            width: 100%;
            margin: 1em 0;
        }
        th, td {
            border: 1px solid #333;  /* 边框样式 */
            padding: 8px 12px;       /* 单元格内边距 */
            text-align: left;
        }
        th {
            background-color: #f0f0f0;  /* 表头背景色 */
        }
        tr:hover {
            background-color: #f5f5f5;
        }
    </style>
    """
    
    # 组合样式和表格内容，形成完整HTML
    full_html = f"""
    <!DOCTYPE html>
    <html>
    <head>
        <meta charset="UTF-8">
        <title>Markdown转换结果</title>
        {table_css}
    </head>
    <body>
        {html_table}
    </body>
    </html>
    """
    
    return full_html

current_dir = os.path.dirname(os.path.abspath(__file__))

# 1. 读取Markdown文件内容
with open(os.path.join(current_dir,"input.md"), "r", encoding="utf-8") as f:
    md_content = f.read()


# 2. 转换为HTML
md_content = md_content.replace('\<br\>','<br>')
html_content = md_to_html_with_styled_tables(md_content)

# 3. 保存为HTML文件
output_filename = 'cheshi'
html_content = re.sub(r"\[cite.*?\]", "", html_content)
html_content = html_content.replace('**','')


with open(os.path.join(current_dir,f"{output_filename}.html"), "w", encoding="utf-8") as f:
    f.write(html_content)

print("转换完成！")